package class03;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * 二叉树的序列化和反序列化
 * 中序遍历，在不同结构存在相同的序列化结构，不满足唯一性
 */
public class Code04_SerializeAndReconstructTree {
    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int data) {
            this.value = data;
        }
    }

    // 先序方式序列化
    static Queue<String> preSerial(Node node) {
        Queue<String> queue = new LinkedList<>();
        pres(node, queue);
        return queue;
    }


    static void pres(Node node, Queue<String> ans) {
        if (null == node) {
            ans.offer(null);
        } else {
            ans.offer(String.valueOf(node.value));
            // 注意子树的null 也要序列化
            pres(node.left, ans);
            pres(node.right, ans);

        }
    }


    static Queue<String> posSerial(Node node) {
        Queue<String> queue = new LinkedList<>();
        poss(node, queue);
        return queue;
    }


    static void poss(Node node, Queue<String> ans) {
        if (null == node) {
            ans.offer(null);
        } else {
            poss(node.left, ans);
            poss(node.right, ans);
            ans.offer(String.valueOf(node.value));

        }
    }


    static Node buildByPreQueue(Queue<String> ans) {
        if (null == ans || ans.isEmpty()) {
            return null;
        }
        return preb(ans);
    }

    static Node preb(Queue<String> ans) {
        String value = ans.poll();
        if (null == value) {
            return null;
        }
        Node head = new Node(Integer.parseInt(value));
        head.left = preb(ans);
        head.right = preb(ans);
        return head;
    }


    static Node buildByPosQueue(Queue<String> ans) {
        if (null == ans || ans.isEmpty()) {
            return null;
        }
        Stack<String> stack = new Stack<>();
        //      反序列化需要先定位头部， 后序遍历：左右中，通过stack 进行调整为中右左
        while (!ans.isEmpty()) {
            stack.push(ans.poll());
        }
        return posb(stack);
    }

    static Node posb(Stack<String> ans) {
        String value = ans.pop();

        if (null == value) {
            return null;
        }

        Node head = new Node(Integer.parseInt(value));
        head.right = posb(ans);
        head.left = posb(ans);

        return head;
    }

    /**
     * 借助队列，当头节点调用叶子节点的时候序列化，借助的队列实现维持树结构
     *
     * @param node
     * @return
     */
    static Queue<String> levelSerial(Node node) {
        if (null == node) {
            return null;
        }

        Queue<String> ans = new LinkedList<>();
        ans.offer(String.valueOf(node.value));

        Queue<Node> helpQueue = new LinkedList<>();
        helpQueue.offer(node);
        while (!helpQueue.isEmpty()) {
            Node cur = helpQueue.poll();
            if (cur.left != null) {
                helpQueue.offer(cur.left);
                ans.offer(String.valueOf(cur.left.value));
            } else {
                ans.offer(null);
            }


            if (cur.right != null) {
                helpQueue.offer(cur.right);
                ans.offer(String.valueOf(cur.right.value));
            } else {
                ans.offer(null);
            }
        }


        return ans;


    }

    static Node buildByLevelQueue(Queue<String> ans) {
        if (null == ans || ans.isEmpty()) {
            return null;
        }
        //1234567nullnullnullnullnullnullnullnull
        // 1.先构造头节点

        Node head = genrateNode(ans.poll());

        Queue<Node> help = new LinkedList<>();
        if (null == help) {
            return null;
        }
        help.offer(head);
        Node curNode;
        while (!help.isEmpty()) {

            curNode = help.poll();
            curNode.left = genrateNode(ans.poll());
            curNode.right = genrateNode(ans.poll());
            // 不为空进队列
            if (curNode.left != null) {
                help.add(curNode.left);
            }
            if (curNode.right != null) {
                help.add(curNode.right);
            }

        }


        return head;

    }

    static Node genrateNode(String val) {
        if (null == val || val.equals("")) {
            return null;
        }
        return new Node(Integer.parseInt(val));
    }


    public static void main(String[] args) {

        Node head = new Node(1);
        head.left = new Node(2);
        head.right = new Node(3);
        head.left.left = new Node(4);
        head.left.right = new Node(5);
        head.right.left = new Node(6);
        head.right.right = new Node(7);


        Queue<String> pre = preSerial(head);
        Queue<String> pos = posSerial(head);


        Node preBuild = buildByPreQueue(pre);
        Node posBuild = buildByPosQueue(pos);

        Queue<String> level = levelSerial(head);
        Node levelBuild = buildByLevelQueue(level);


    }
}
